﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CP.Entities;
using CP.Controllers.GPM;
using DevExpress.XtraEditors;
using CP.Utility;

namespace GPM
{
    public partial class FrmInitWebpart : BaseEditForm
    {
        WebpartController controller = new WebpartController();
        public int CurrentWebpartID { get; private set; }
        public FrmInitWebpart()
        {
            InitializeComponent();
        }

        public FrmInitWebpart(string id, int Webpartid)
            : this()
        {
            base.EntityID = id;
            this.CurrentWebpartID = Webpartid;
        }

        /// <summary>
        /// 编辑或者保存状态下取值函数
        /// </summary>
        /// <param name="info"></param>
        private void SetInfo(CP.Entities.Webpart info)
        {
            info.webpart_id = string.IsNullOrEmpty(this.txtWebpartID.Text.Trim()) ?
                                0 : Convert.ToInt32(this.txtWebpartID.Text.Trim());
            info.module_id = Convert.ToInt32(this.lookupParentID.EditValue);
            info.form_name = this.txtFormName.Text.Trim();
            info.form_text = this.txtFormText.Text.Trim();
        }

        /// <summary>
        /// 数据字典加载
        /// </summary>
        private void InitDictItem()
        {
            ModuleController mc = new ModuleController();
            this.lookupParentID.Properties.DataSource = mc.GetAllModules();
        }

        public override bool CheckInput()
        {
            //if (string.IsNullOrEmpty(this.txtWebpartID.Text.Trim()))
            //{
            //    ExMessageBox.Show("提示信息：组件ID不能为空！", AppSetter.SoftName);
            //    return false;
            //}

            if (this.lookupParentID.EditValue == null)
            {
                ExMessageBox.Show("提示信息：模块名称不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtFormName.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：窗体Name不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtFormText.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：窗体标题不能为空！", AppSetter.SoftName);
                return false;
            }

            return true;
        }

        public override void DisplayData()
        {
            InitDictItem();

            if (!string.IsNullOrEmpty(EntityID))
            {
                CP.Entities.Webpart info = controller.GetWebpartByID(this.CurrentWebpartID);
                #region 显示菜单信息

                this.txtWebpartID.Focus();
                if (null != info)
                {
                    this.txtWebpartID.Text = info.webpart_id.ToString();
                    this.txtWebpartID.Enabled = false;
                    this.txtFormText.Text = info.form_text;
                    this.lookupParentID.EditValue = info.module_id;
                    this.txtFormName.Text = info.form_name;
                }

                #endregion
            }
            else
            {
                this.txtWebpartID.Focus();
            };
        }

        public override bool SaveAddNew()
        {
            CP.Entities.Webpart webpart = new CP.Entities.Webpart();
            this.SetInfo(webpart);
            controller.AddWebpart(webpart);
            LogHelper.Log("组件管理", "新增", "", "webpart:" +
                webpart.webpart_id.ToString());
            (this.Owner.ActiveMdiChild as FrmWebpart).RefreshWebpart();
            return true;
        }

        public override bool SaveUpdated()
        {
            CP.Entities.Webpart webpart = new CP.Entities.Webpart();
            this.SetInfo(webpart);
            controller.EditWebpart(webpart);
            LogHelper.Log("组件管理", "编辑", "", "webpart:" +
             webpart.webpart_id.ToString());
            (this.Owner.ActiveMdiChild as FrmWebpart).RefreshWebpart();
            return true;
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "控件文件 (*.ascx)|*.ascx";
                ofd.InitialDirectory = Application.StartupPath;
                ofd.Title = "请选择用户控件文件";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if (String.IsNullOrEmpty(ofd.FileName))
                    {
                        ExMessageBox.Show("没有选中文件!", AppSetter.SoftName);
                        return;
                    }

                    string filename = System.IO.Path.GetFileName(ofd.FileName);
                    if (string.IsNullOrEmpty(filename))
                    {
                        ExMessageBox.Show("文件名有误!", AppSetter.SoftName);
                        return;
                    }
                    this.txtFormText.Text = filename;
                    this.txtFormText.Tag = ofd.FileName;
                }
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }
    }
}
